const getMysql = require('../mysql.js')

// 社区分页和总数查询
const community = async (req, res) => {
    // let mid =  req.query.mid
    let pno = Number((req.query.mid - 1) * 8)
    let sql = "select * from home_recom  limit ?,8";
    let sql2 = "select count(*) as num from home_recom"
    getMysql(sql, pno).then(result => {

        getMysql(sql2).then(counts => {
            res.send({
                coded: 200,
                num: counts,
                message: '查询成功！！',
                data: result,
                pages: Math.ceil(Number(counts[0].num) / 8)
            })
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}

// 社区分页和总数查询
const communityNew = async (req, res) => {
    // let mid =  req.query.mid
    let pno = Number((req.query.mid - 1) * 8)
    let sql = "select * from home_recom order by zan limit ?,8";
    let sql2 = "select count(*) as num from home_recom"
    getMysql(sql, pno).then(result => {

        getMysql(sql2).then(counts => {
            res.send({
                coded: 200,
                num: counts,
                message: '查询成功！！',
                data: result,
                pages: Math.ceil(Number(counts[0].num) / 8)
            })
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}


// 跳转详情页接口
const communDetail = async (req, res) => {
    let mid = Number(req.query.mid)
    let sql = 'select * from community_detail,home_recom where home_recom.mid = ? and home_recom.mid=community_detail.mid'
    getMysql(sql, mid).then(result => {
        res.send({
            coded: 200,
            message: '查询成功！！',
            data: result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            coded: 201,
            message: '查询失败',
        })
    })
}

// 社区收藏
const collect = async (req, res) => {
    let sql2 = 'select mid from community_collect where mid = ? and username = ?'
    getMysql(sql2, req.body.mid, req.body.username).then(result => {
        if (result.length == 0) {
            collectSearch()
        } else {
            res.send({
                coded: 205,
                message: '该帖子已经收藏！！'
            })
        }
    })

    // 收藏前查询mid
    function collectSearch() {
        let { username, image, title, mid, author } = req.body;
        let sql = 'insert into community_collect(mid,title,image,author,username) values (?,?,?,?,?)'
        getMysql(sql, mid, title, image, author, username).then(result => {
            res.send({
                coded: 200,
                message: '插入成功！！！'
            })
        }).catch(err => {
            console.log(err);
            res.send({
                coded: 201,
                message: '插入失败',
            })
        })

    }
}

// 取消社区收藏
const uncollect = async (req, res) => {
    let mid = req.body.mid
    let username = req.body.username
    let sql = 'delete from community_collect where mid=? and username = ?'
    getMysql(sql, mid, username).then(result => {
        console.log(result);
        res.send({
            coded: 200,
            message: '删除收藏成功！'
        })
    }).catch(err=>{
        res.send({
            coded: 202,
            message: '删除收藏失败'
        })
    })
}

// 社区模糊查询
const trickSearch = async (req, res) => {
    // let title = "%" + JSON.stringify(req.body.title).replace(/\"/g, "") + "%"
    let title = "%" + req.body.title + "%"
    console.log(title);
    let sql = `select * from home_recom where title like ? or lable like ?`
    getMysql(sql, title,title).then(result => {
        res.send({
            code: 200,
            message: '查询成功',
            data:result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}



module.exports = {
    community,
    communDetail,
    collect,
    communityNew,
    trickSearch,
    uncollect
}